/**
 * Created by hxpeng on 2017/3/25.
 */
(function () {
    'use strict';

    angular.module('App')
        .run(authRun)
        .run(cordova);

    //前端全局拦截登录
    function authRun($rootScope,$state,Global,AppUtils,$location,LoginAccountAuth,$window,$timeout) {

        $rootScope.$on('$ionicView.beforeEnter', function(){
            $("#loading").css("display","block");

        });

        $rootScope.$on('$ionicView.afterEnter', function(){
            $("#loading").fadeOut();
        });



        $rootScope.$on('$locationChangeStart', function (event, next, current) {
            var url  = $location.path();
            console.log(url);
            var postfix = -1;
            var pathStr;
            //路径?和#不能共存
            if(url.indexOf('?') != -1){
                postfix = url.indexOf('?');
            }
            if(url.indexOf('#') != -1){
                postfix = url.indexOf('#');
            }
            if(postfix != -1){
                pathStr = url.substr(0, postfix);
            }else{
                pathStr = url;
            }
            var keepGoing = true;
            //判断路径是否被拦截
            angular.forEach(Global.filterPath,function(data){
                if(pathStr == data){
                    keepGoing = false;
                    return;
                }
            })
            if(keepGoing){
                if(!LoginAccountAuth.isLoginIn()){
                    $location.replace();
                    var gotoUrl = LoginAccountAuth.getLastGotoUrl();
                    if(AppUtils.isUndefinedOrNull(gotoUrl) && AppUtils.isUndefinedOrNull(gotoUrl) && url != '/'){
                        LoginAccountAuth.setLastGotoUrl(url);
                        gotoUrl = url;
                    }
                    LoginAccountAuth.getAccount()
                        .then(function (data) {
                            var nextPath;
                            if(AppUtils.isUndefinedOrNull(data.memberId)){
                                nextPath = "/bind/menu";
                            }else{
                                if(!AppUtils.isUndefinedOrNull(gotoUrl) && gotoUrl != "/"){
                                    nextPath = gotoUrl;
                                    LoginAccountAuth.setLastGotoUrl(null);
                                    //加个参数解决angular不会重新请求。。。
                                    $location.path(nextPath).search({a: "111"});
                                    event.preventDefault();
                                }else{
                                    if(data.memberType == 2){
                                        nextPath = "/member/parentIndex";
                                    }else if(data.memberType == 1){
                                        nextPath = "/class/teacherIndex";
                                    }
                                }
                            }
                            $location.path(nextPath);
                        });
                    event.preventDefault();
                }else{
                    if(url === "/"){
                        LoginAccountAuth.getAccount()
                            .then(function (data) {
                                if(data.memberType == 2){
                                    $location.path("member/parentIndex");
                                }else if(data.memberType == 1){
                                    $location.path("class/teacherIndex");
                                }
                            });
                        event.preventDefault();
                    }
                }
            }
        });


        //监听路由变化之前
        $rootScope.$on('$stateChangeStart',function(event,toState,toParams,fromState,fromParams) {
            //layer.closeAll();
        })

        //监听路由变化成功
        $rootScope.$on('$stateChangeSuccess',function(event,toState,toParams,fromState,fromParams){
        });

        //监听路由未找到
        $rootScope.$on('$stateNotFound',function(event,toState,toParams,fromState,fromParams){
        });
    }

    function cordova($ionicPlatform){
        $ionicPlatform.ready(function() {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if(window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if(window.StatusBar) {
                StatusBar.styleDefault();
            }
        });
    }



})( );